package qf.common.security.service;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import qf.common.core.web.vo.AjaxResult;
import qf.common.security.entitiy.SecuriyUserDetailVO;
import qf.common.security.entitiy.SysUser;

import javax.annotation.Resource;
import java.util.List;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SecuritySysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        AjaxResult result = userService.loginUserByUserNameAndPassowrd(username,"");

        if (result.isError()) {
            throw new RuntimeException("账号或密码错误");
        }
        // 存在数据的情况下
        SysUser user = JSONObject.parseObject(JSONObject.toJSONString(result.getResultData()),SysUser.class);
        //传递用户所具有的权限

        List<String> list = user.getRightsName();
        return new SecuriyUserDetailVO(user,list);
    }
}

